package com.fcscs.cservice.client.main.reservation;

import java.util.List;

import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.Label;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.ListView;
import com.extjs.gxt.ui.client.widget.form.AdapterField;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.fcscs.core.gwt.common.CoreConstants;
import com.fcscs.core.gwt.common.FormSubmitListener;
import com.fcscs.core.gwt.common.ui.BaseFormDialog;
import com.fcscs.core.gwt.common.ui.FcsFormPanel;
import com.fcscs.core.gwt.common.ui.FcsFields.FormButton;
import com.fcscs.cservice.client.common.CServiceConstants;
import com.fcscs.cservice.client.common.model.reservation.MessageModelData;
import com.fcscs.cservice.domain.model.MessageView;
import com.fcscs.cservice.domain.model.ReservationView;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;

public class MessageDialog extends BaseFormDialog<ReservationView> {
	private MyFormPanel formPanel;
	private static MessageDialog INSTANCE;
	private List<MessageView> messageList;
	
	public static MessageDialog getInstance(FormSubmitListener<ReservationView> listener,
			List<MessageView> messageList){
		if(null == INSTANCE){
			INSTANCE = new MessageDialog();
		}
		INSTANCE.messageList = messageList;
		INSTANCE.setHeading(CoreConstants.CORE_CONSTANTS.message());
		return INSTANCE;
	}
	
	private MessageDialog() {
		super();
		formPanel = new MyFormPanel(null);
		formPanel.setId("MessageDialog_formPanel");
		add(formPanel);
		setSize(540, 300);
	}

	private class MyFormPanel extends FcsFormPanel<ReservationView>{
		private Label unreadMsgLabel;
		private AdapterField unreadMsgAF;
		
		private Label totalMsgLabel;
		private AdapterField totalMsgAF;
		
		private Label allMessageLabel;
		
		private ListView<MessageModelData> messageListView;
		private ListStore<MessageModelData> messageListStore;
		
		private Label fromLabel;
		private AdapterField fromAF;
		
		private Label sentLabel;
		private AdapterField sentAF;
		
		private Label subjectLabel;
		private AdapterField subjectAF;
		
		private TextArea contentArea;
		
		private FormButton okButton;

		protected MyFormPanel(FormSubmitListener<ReservationView> l) {
			super(l);
			BorderLayout bl = new BorderLayout();
			bl.setContainerStyle("backgroundColor:white");
			setLayout(bl);
			setHeaderVisible(false);
			setSize(530, 260);
			
			LayoutContainer lcWest = new LayoutContainer();
			lcWest.setId("MessageDialog_lcWest");
			FormLayout layout = new FormLayout();
			layout.setLabelAlign(LabelAlign.LEFT);
			lcWest.setLayout(layout);
		    lcWest.setEnabled(true);
		    
		    unreadMsgLabel = new Label();
		    unreadMsgLabel.setId("MessageDialog_unReadMsgLabel");
			
		    unreadMsgAF = new AdapterField(unreadMsgLabel);
		    unreadMsgAF.setId("MessageDialog_unReadMsgAF");
		    unreadMsgAF.setFieldLabel(CServiceConstants.CSERVICE_CONSTANTS.unreadMessage());
		    lcWest.add(unreadMsgAF);
		    
		    totalMsgLabel = new Label();
		    totalMsgLabel.setId("MessageDialog_totalMsgLabel");
			
		    totalMsgAF = new AdapterField(totalMsgLabel);
		    totalMsgAF.setId("MessageDialog_totalMsgAF");
		    totalMsgAF.setFieldLabel(CServiceConstants.CSERVICE_CONSTANTS.totalMessage());
		    lcWest.add(totalMsgAF);
		    
		    allMessageLabel = new Label("<html>"+"<h3>"+CServiceConstants.CSERVICE_CONSTANTS.allMessages()+"</h3>"+"</html>");
		    allMessageLabel.setId("MessageDialog_allMessageLabel");
		    lcWest.add(allMessageLabel);
		    
		    messageListStore = new ListStore<MessageModelData>();
			messageListView = new ListView<MessageModelData>();
			messageListView.setId("MessageDialog_messageListView");
			messageListView.setStore(messageListStore);
			messageListView.setDisplayProperty(CServiceConstants.SUBJECT);
			messageListView.getSelectionModel().addListener(Events.SelectionChange, 
					new Listener<SelectionChangedEvent<MessageModelData>>(){
						@Override
						public void handleEvent(
								SelectionChangedEvent<MessageModelData> be) {
							if (be.getSelectedItem() != null) {
								fromLabel.setText(be.getSelectedItem().getEntity().getMsgFrom());
								sentLabel.setText(CServiceConstants.DATE_FORMAT4.format(be.getSelectedItem().getEntity().getMsgDateTime()));
								subjectLabel.setText(be.getSelectedItem().getEntity().getMsgSubject());
								contentArea.setVisible(true);
								contentArea.setValue(be.getSelectedItem().getEntity().getMsgContent());
							}
						}
			});
		    
		    lcWest.add(messageListView);
			
		    LayoutContainer lcCenter = new LayoutContainer();
		    lcCenter.setId("MessageDialog_lcCenter");
		    layout = new FormLayout();
			layout.setLabelAlign(LabelAlign.LEFT);
			lcCenter.setLayout(layout);
			lcCenter.setBorders(true);
		    
		    fromLabel = new Label();
		    fromLabel.setId("MessageDialog_fromLabel");
			
		    fromAF = new AdapterField(fromLabel);
		    fromAF.setId("MessageDialog_fromAF");
		    fromAF.setFieldLabel(CoreConstants.CORE_CONSTANTS.from());
		    lcCenter.add(fromAF);
		    
		    sentLabel = new Label();
		    sentLabel.setId("MessageDialog_sentLabel");
			
		    sentAF = new AdapterField(sentLabel);
		    sentAF.setId("MessageDialog_sentAF");
		    sentAF.setFieldLabel(CoreConstants.CORE_CONSTANTS.send());
		    lcCenter.add(sentAF);
		    
		    subjectLabel = new Label();
		    subjectLabel.setId("MessageDialog_subjectLabel");
			
		    subjectAF = new AdapterField(subjectLabel);
		    subjectAF.setId("MessageDialog_subjectAF");
		    subjectAF.setFieldLabel(CoreConstants.CORE_CONSTANTS.subject());
		    lcCenter.add(subjectAF);
		    
		    contentArea = new TextArea();
		    contentArea.setId("MessageDialog_textArea");
		    contentArea.setReadOnly(true);
		    contentArea.setLabelSeparator("");
		    lcCenter.add(contentArea);
		    
		    add(lcCenter, new BorderLayoutData(LayoutRegion.CENTER));
		    add(lcWest,new BorderLayoutData(LayoutRegion.WEST));
		    
		    okButton = new FormButton(CoreConstants.CORE_CONSTANTS.ok());
		    okButton.setId("MessageDialog_okButton");
			okButton.addSelectionListener(new SelectionListener<ButtonEvent>(){
				@Override
				public void componentSelected(ButtonEvent ce) {
					MessageDialog.this.hide();
				}
			});
			
			getButtonBar().add(okButton);
		}

		@Override
		public void updateForm(ReservationView entity) {
			messageListStore.removeAll();
			for(MessageView m : messageList){
				messageListStore.add(new MessageModelData(m));
			}
			
			unreadMsgLabel.setText(entity.getUnreadMessageNum() + "");
			totalMsgLabel.setText(entity.getMessageNum() + "");
			
			Scheduler.get().scheduleFinally(new ScheduledCommand() {
				@Override
				public void execute() {
					if (messageList != null && messageList.size()>0) {
						fromLabel.setText(messageList.get(0).getMsgFrom());
						sentLabel.setText(CServiceConstants.DATE_FORMAT4.format(messageList.get(0).getMsgDateTime()));
						subjectLabel.setText(messageList.get(0).getMsgSubject());
						contentArea.setValue(messageList.get(0).getMsgContent());
					}
				}
			});
		}

		@Override
		public void populateEntity(ReservationView entity) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void setFocus() {
			// TODO Auto-generated method stub
			
		}
	}
	
	@Override
	protected FcsFormPanel<ReservationView> getFormPanel() {
		return formPanel;
	}

}
